package com.zhangtai.modules.controller.admin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.zhangtai.modules.entity.qw.QwCheckinMonthEntity;
import com.zhangtai.modules.service.qw.QwCheckinMonthService;
import com.zhangtai.modules.service.qw.QwCheckinsService;
import com.zhangtai.utils.PageUtils;
import com.zhangtai.utils.R;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;


/**
 * @author wanwgei
 * @email i@weiwang.com
 * @date 2021-05-31 18:40:47
 */
@RestController
@RequestMapping("coupon/qwcheckinmonth")
public class QwCheckinMonthController {
    @Autowired
    private QwCheckinMonthService qwCheckinMonthService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = qwCheckinMonthService.queryPage(params);

        return R.ok(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        QwCheckinMonthEntity qwCheckinMonth = qwCheckinMonthService.getById(id);
        return R.ok(qwCheckinMonth);
    }

    /**
     * 保存
     * 定时任务,  每个月的1号拉取上一个月的完整数据数据来存储
     */
    @Scheduled(cron = "0 0 0 1 * ? ")
    @RequestMapping("/save")
    public R save() throws ParseException {
        DateTime dateTime = DateUtil.lastMonth();
        int year = dateTime.year();
        int month = dateTime.month();
        int lastDay = DateUtil.yesterday().dayOfMonth();
        String start_Times=null;
        String end_Times=null;
        if(month<10){
             start_Times = year + "-0" + month + "-" + "01" + " " + "00:00:00";
             end_Times = year + "-0" + month + "-" + lastDay + " " + "00:00:00";
        }else{
             start_Times = year + "-" + month + "-" + "01" + " " + "00:00:00";
             end_Times = year + "-" + month + "-" + lastDay + " " + "00:00:00";
        }
        String [] userids=null;
        Date s=(Date)new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parseObject(start_Times);
        int startTimes = (int) (s.getTime() / 1000);
        Date e=(Date)new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parseObject(end_Times);
        int endTimes = (int) (e.getTime() / 1000);
        qwCheckinMonthService.saveCheckMonth(startTimes,endTimes,userids);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody QwCheckinMonthEntity qwCheckinMonth) {
        qwCheckinMonthService.updateById(qwCheckinMonth);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("coupon:qwcheckinmonth:delete")
    public R delete(@RequestBody Integer[] ids) {
        qwCheckinMonthService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
